// 最大区间乘积问题  区间中的最小数 * 区间所有数的和 最大

// a = [3, 7, 2, 6, 8]   84


function solution(n, a) {
  let maxResult = 0

  for (let i = 0; i < n; i++) {
    let minVal = a[i]

    let left = i
    while (left > 0 && a[left - 1] >= minVal) {
      left--
    }

    let right = i
    while (right < n - 1 && a[right + 1] >= minVal) {
      right++
    }

    let num = 0
    for (let i = left; i <= right; i++) {
      num += a[i]
    }

    maxResult = Math.max(maxResult, num * minVal)
  }

  return maxResult
}

function main() {
  console.log(solution(3, [6, 2, 1]) === 36);
  console.log(solution(4, [5, 1, 4, 3]) === 25);
  console.log(solution(5, [7, 3, 2, 1, 8]) === 64);
}

main();